﻿using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using FranceTriple.Dal;
using System.Data;

namespace TeamFranceTriple
{
    public partial class LastTopics : BaseUserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            System.IO.StringReader xmlSR = null;

            //List<Int32> ForumIdAllowed = UserAccessDal.ForumIdAllowed((Session["AnonymousID"] == null) ? String.Empty : Session["AnonymousID"].ToString());
            //if (ForumIdAllowed.Count > 0)
            //    LastTopicsEntityDataSource.Where = "it.ForumCategory.Forum.Id IN {" + String.Join(",", ForumIdAllowed.ToArray()) + "}";
            //else
            //    LastTopicsEntityDataSource.Where = "false";

            xmlSR = new System.IO.StringReader(ForumTopicView.GetLastPostCreateXmlEntities((Session["AnonymousID"] == null) ? String.Empty : Session["AnonymousID"].ToString(), ThisMaster.Requester.Profile.TimeZone));
            
            ds.ReadXml(xmlSR);
            LastTopicsListView.DataSource = ds.Tables["Topic"];
            LastTopicsListView.DataBind();



        }

        protected void LastTopicsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            HyperLink forumPostHyperLink;
            ListView LastPostListView;
            DataRowView drv;
            ListViewItem item = e.Item;
            DataView view = null;

            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                // ForumPostHyperLink.NavigateUrl contient ForumTopicId
                forumPostHyperLink = (HyperLink)item.FindControl("ForumPostHyperLink");

                var targetpostlink = item.FindControl("PostTargetHyperLink") as HyperLink;
                if (targetpostlink != null)
                {
                    targetpostlink.NavigateUrl = "~/" + ThisMaster.Requester.Culture + "/forumpost/" + Int32.Parse(forumPostHyperLink.NavigateUrl);
                }

                if (forumPostHyperLink != null)
                {
                    forumPostHyperLink.NavigateUrl = "~/" + ThisMaster.Requester.Culture + "/forumpost/" + forumPostHyperLink.NavigateUrl +
                        "/?topicpagenumber=" + ((Page.RouteData.Values["topicPageNumber"] != null) ? Page.RouteData.Values["topicPageNumber"].ToString() : String.Empty);
                }

                LastPostListView = (ListView)item.FindControl("LastPostListView");
                drv = (DataRowView)item.DataItem;

                if (drv != null && drv.DataView.Table.ChildRelations.Count > 0)
                    view = drv.CreateChildView("Topic_LastPost");

                if (view != null)
                {
                    LastPostListView.DataSource = view;
                    LastPostListView.DataBind();
                }
            }
        }

        protected void LastPostListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            TextBox forumCategoryIdLabel;
            HyperLink lastPostHyperLink;

            forumCategoryIdLabel = (TextBox)((ListViewItem)e.Item).FindControl("ForumCategoryIdLabel");
            lastPostHyperLink = (HyperLink)((ListViewItem)e.Item).FindControl("LastPostHyperLink");
            
            if (lastPostHyperLink != null && forumCategoryIdLabel != null)
                lastPostHyperLink.NavigateUrl = "~/" + ThisMaster.Requester.Culture + "/forumpost/showpost/" + ((DataRowView)e.Item.DataItem).Row.ItemArray[3].ToString() +
                    "/?topicpagenumber=" + ((Page.RouteData.Values["topicPageNumber"] != null) ? Page.RouteData.Values["topicPageNumber"].ToString() : String.Empty);
        }
    }
}